<template>
  <div class="mail-item">
    <div class="headerImg">
      <img
        v-if="getUserPhoto"
        :src="getUserPhoto"
        alt=""
      >
      <span v-else>
        {{ mailItem.senderUserName.substring((mailItem.senderUserName.length-2),mailItem.senderUserName.length) }}
      </span>

      <div class="name">
        {{ mailItem.senderUserName || $t('m.weiyou_unknow') }}
      </div>
    </div>
    <div class="mail-content">
      <div
        @click="openDetail"
        class="mail-theme"
      >
        <h4
          :style="{'width': themeWidth, 'color': mailItem.msgRead && mailItem.read ? '#999' :'#050202'}"
          class="mail-theme-cnt"
        >
          <span class="cont">{{ mailItem.zt || $t('m.weiyou_nosubjectcontent') }}</span>

          <i
            v-if="mailItem.msgFile"
            class="jeicon jeicon-attachment-o hasFile"
          />
        </h4>
        <span class="time">
          {{ formatTime(mailItem.updateTime) }}
        </span>
      </div>

      <p class="mail-reply">
        <el-badge
          :type="mailItem.msgRead ? 'info' : 'danger'"
          is-dot="true"
        >
          <p
            @click="openDetail"
            :style="{'width': themeWidth}"
            v-html="handleImg || $t('m.weiyou_nocomment')"
            class="reply"
          >
            <!-- {{ handleImg || $t('m.weiyou_nocomment') }} -->
          </p>
        </el-badge>
        <span class="mail-handle">
          <i
            @click="clickLike"
            v-if="!mailItem.zan"
            class="jeicon jeicon-thumbs-up-o"
          />
          <i
            @click="clickLike"
            v-if="mailItem.zan"
            style="color:#f7ba2a"
            class="jeicon jeicon-thumbs-up"
          />
          <span>{{ mailItem.dzNum }}</span>
          <i
            @click="clickFocus"
            v-if="!mailItem.focus"
            class="fe icon-heart-empty-1"
          />
          <i
            @click="clickFocus"
            v-if="mailItem.focus"
            style="color:#FF504F"
            class="fe icon-heart-1"
          />
        </span>
      </p>

      <p class="mail-tools">
        <span class="mail-bottom">
          <slot name="tools" />
        </span>
      </p>
    </div>
    <mail-detail
      ref="mailDetail"
      v-on="$listeners"
      :mailDetail="mailItem"
    />
  </div>
</template>
<script>

import moment from 'moment';
import 'moment/locale/zh-cn';
import MailDetail from './detail';
import { addReadMicromail } from '../actions/actions';
import returnMsg from '../assets/lang/speNationalList';

export default {
  name: 'MailItem',
  components: { MailDetail },
  props: {
    mailItem: {
      type: Object,
      default: () => { },
    },
    themeWidth: {
      type: String,
      default: '450px',
    },
  },
  data() {
    return {
      zanCount: 0,
      focusCount: 0,
    };
  },
  computed: {
    getUserPhoto() {
      return JE.getUserPhoto(this.mailItem.sendUserId);
    },
    handleImg() {
      return this.mailItem.lastMsg && this.mailItem.lastMsg.replace(/<\s?img[^>]*>/gi, '[图片]');
    },
  },
  mounted() {
    // 人员头像处理
    const me = this;
    if (!JE.isNotEmpty(JE.__USERS__)) {
      $(document).on('mysubscrible', () => {
        me.$emit('updatePhoto');
      });
    }
  },
  methods: {
    // 格式化日期
    formatTime(time) {
      const d = moment(time).fromNow();
      if (d.indexOf('天') !== -1) {
        return time.substr(0, 10);
      }
      return d;
    },
    // 查看详情
    openDetail() {
      this.$refs.mailDetail.detailVisible = true;
    },
    // 点赞
    clickLike() {
      if (this.zanCount === 6) {
        JE.msg(returnMsg('操作频繁，请五分钟后再试'));
        return false;
      }
      this.zanCount++;

      if (this.zanCount === 6) {
        setTimeout(() => {
          this.zanCount = 0;
        }, 1000 * 60 * 5);
      }

      const params = {
        type: '2',
        id: this.mailItem.id,
      };

      addReadMicromail(params).then(() => {
        this.mailItem.zan = !this.mailItem.zan;
        this.mailItem.dzNum = Number(this.mailItem.dzNum) + (this.mailItem.zan ? 1 : -1);
        if (this.mailItem.dzNum < 0) {
          this.mailItem.dzNum = 0;
        }
      });
    },
    // 收藏关注
    clickFocus() {
      if (this.focusCount === 6) {
        JE.msg(returnMsg('操作频繁，请五分钟后再试'));
        return false;
      }
      this.focusCount++;

      if (this.focusCount === 6) {
        setTimeout(() => {
          this.focusCount = 0;
        }, 1000 * 60 * 5);
      }
      const params = {
        type: '3',
        id: this.mailItem.id,
      };

      addReadMicromail(params).then((data) => {
        if (data.success) {
          // this.mailItem.isFocus = !this.mailItem.isFocus;
          this.$set(this.mailItem, 'focus', !this.mailItem.focus);
          this.$emit('doFocus'); // 刷新列表  和角标
        }
      });
    },
  },
};
</script>
<style lang="scss">
.mail-item {
  overflow: hidden;
  padding: 20px;
  padding-right: 22px;
  padding-bottom: 0;
  padding-top: 5px;
  .headerImg {
    width: 36px;
    padding-top: 2px;
    float: left;
    img {
      width: 100%;
      height: 36px;
      border-radius: 50%;
    }
    span {
      display: inline-block;
      width: 36px;
      height: 36px;
      border-radius: 50%;
      overflow: hidden;
      text-align: center;
      background-color: #efa544;
      line-height: 37px;
      color: #fff;
      font-size: 12px;
    }
    .name {
      color: #a9aba9;
      font-size: 12px;
      text-align: center;
    }
  }
  .mail-content {
    float: right;
    padding-left: 15px;
    padding-top: 5px;
    padding-bottom: 7px;
    border-bottom: 1px solid #f3f3f3;
    p {
      font-size: 12px;
      font-weight: 400;
      color: rgba(169, 171, 169, 1);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .mail-theme {
      overflow: hidden;
      margin-bottom: 15px;
      align-items: center;
      cursor: pointer;
      .mail-theme-cnt {
        float: left;
        font-size: 14px;
        font-weight: 500;
        color: rgb(5, 2, 2);
        width: 465px;
        overflow: hidden;
        align-items: center;
        height: 19px;
        .cont{
          display: block;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          &:hover {
            color: #2f6bcf;
          }
        }
        .hasFile{
          color: #ff6f45;
          margin-left: 5px;
          font-size: 14px;
        }
      }
      .time {
        float: right;
        color: #a9aba9;
        text-align: right;
      }
    }
    .mail-reply {
      overflow: hidden;
      margin-bottom: 0;
      .reply {
        width: 465px;
        height: 18px;
        cursor: pointer;
        padding-left: 15px;
      }
      .reply:hover {
        color: #2f6bcf;
      }
      .mail-handle {
        text-align: right;
        font-size: 14px;
        i {
          cursor: pointer;
          font-size: 14px;
        }
        span {
          display: inline-block;
          min-width: 20px;
          text-align: left;
        }
      }
    }
    .el-badge__content.is-fixed {
      top: 50%;
      left: 0px;
      height: 8px;
      width: 8px;
      padding: 0;
      transform: translateY(-50%);
    }
    .el-badge__content--danger{
      background-color: #d24d57;
    }
    .el-badge__content--info{
      background-color: #e4e4e4;
    }
    .mail-tools {
      padding-top: 10px;
      overflow: hidden;
      display: flex;
      align-items: center;
      .mail-bottom {
        float: left;
        font-size: 13px;
        font-weight: 400;
        color: rgba(169, 171, 169, 1);
        .mail-module {
          display: inline-block;
          width: 68px;
          height: 24px;
          line-height: 24px;
          margin-right: 10px;
          cursor: pointer;
        }
      }
      .mail-bottom:hover {
        color: #2f6bcf;
      }
    }
  }
  &:last-child .mail-content {
    border: 0;
  }
}
</style>>
